#include"Enemy.h"
#include "GameObject.h"
#include "BaseEntity.h"
#include "Model.h"
#include "Camera.h"
#include "GlobalDefines.h"
#include "BSPTree.h"
#include "DXConsole.h"
#include "MessageHandler.h"
#include <math.h>

int num = 0;
float numx = 0, numz = 0;
float deltaTime = 0, accumulator = 0, accum2 = 0, delayTime = 2;
float enemydead = 0.0f;

Enemy::~Enemy()
{
	if(m_pEnemyState != NULL)
	{
		delete m_pEnemyState;
		m_pEnemyState = NULL;
	}

	if(m_Model != NULL)
	{
		delete m_Model;
		m_Model = NULL;
	}

	releaseSounds();
}

void Enemy::render(LPDIRECT3DDEVICE9 device)
{
	 // Render 
	 m_Model->render(dxMgr.getDevice());
}

void Enemy::move(int direction, float anim_rate)
{
	m_VectorRotation.z += 0.03f;
}

void Enemy::update(float a_dt)
{	
	m_dt = a_dt;
	setOld();
	updateFSM();
}

bool Enemy::HandleMessage(const Telegram& msg)
{
	bool ihandled;
	if(msg.Msg==MSG_COLLIDE)
	{
		revertToOld();
		ihandled = true;
	}
	if(msg.Msg == MSG_AWARE)
	{
		SetTrigState(2);	// Function that triggers state changes
		ihandled = true;
	}

	if(msg.Msg == MSG_ATKCOLLIDE)
	{
		DmgRcv(50);
	}

	if(msg.Msg == MSG_ATK_AWR)
	{
		SetTrigState(2);	// Function that triggers state changes
		DmgRcv(50);
	}

	if(msg.Msg == MSG_PRJCOLLIDE)
	{
		SetTrigState(2);	// Function that triggers state changes
		DmgRcv(0.5);
	}

	return ihandled;
}

// update the state machine of the enemy
void Enemy::updateFSM()
{
	if(m_pEnemyState)
	{
		m_pEnemyState->Update();
	}
}

bool Enemy::create(LPDIRECT3DDEVICE9 device, std::string modelName)
{
	m_position.x = 0.0f;
	m_position.y = 0.0f;
	m_position.z = 0.0f;

	m_VectorRotation.x = 0.0f;//hip to hip
	m_VectorRotation.y = 0.0;//Crotch to butt
	m_VectorRotation.z = 0.0f;//Head to Foot
	setOld();

	m_radius = 25;
	m_meleeRad = 100;
	m_radarRad = 350; // Radius for the enemy's invisble radar

	setHP(200); // sets the hp to 100;

	srand((unsigned)time(0));

	m_BuffAwr = dxAmgr.LoadWaveToSoundBuffer("Combat.wav");
	m_BuffAtk = dxAmgr.LoadWaveToSoundBuffer("punch2.wav");
	m_BuffDie = dxAmgr.LoadWaveToSoundBuffer("enemies.wav");

	m_Model = new Model();
	return m_Model->loadModel(device,modelName);
}


// Function to move the AI
void Enemy::enemyAI()
{
	D3DXVECTOR3 Oldpos = m_position;

	deltaTime = m_dt;

	accum2 += deltaTime;

	if(accum2 > delayTime)
	{
		m_disp = GetCoord() - m_position;

		//getting direction
		D3DXVec3Normalize(&m_direction, &m_disp);

		accum2 = 0;
	}

	m_polarDelta = polarConversion2D(m_direction);

	m_rotz = m_VectorRotation.z;

	if(m_polarDelta > 0)
	{
		m_VectorRotation.z += 90.0f * m_dt;
	}

	if(m_polarDelta < 0)
	{
		m_VectorRotation.z -= 90.0f * m_dt;
	}
	
	m_position.x += (m_magnitude.x * m_direction.x) * m_dt;
	m_position.z += (m_magnitude.z * m_direction.z) * m_dt;

	m_position.y = 0;
}

D3DXVECTOR3 Enemy::GetCoord()
{
	const int arraysize = 1;
	WayPoint	waypt[arraysize];

	num = (rand() % 4);
	
	numx = (rand() % 1000+1);
	numz = (rand() % 1000+1);

	if(num == 0)
	{
		numx; numz;
	}
	
	if(num == 1)
	{
		numx *= -1; numz;
	}

	if(num == 2)
	{
		numx; numz *= -1;
	}

	if(num == 3)
	{
		numx *= -1; numz *= -1;
	}

	waypt[0].m_direct.x = numx;
	waypt[0].m_direct.z = numz;

	return waypt[0].m_direct;
}

void Enemy::ChasePlayer()
{
	Handler->SendMessage(0,1,0,MSG_ATK,NULL);

	D3DXVECTOR3 Oldpos = m_position;

    m_disp = m_playerinfo - m_position;

	//getting direction
    D3DXVec3Normalize(&m_direction, &m_disp);

	m_polarDelta = polarConversion2D(m_direction);

	//m_rotz += m_VectorRotation.z;

	if(m_VectorRotation.z <= m_polarDelta)
	{
		m_VectorRotation.z += 90.0f * m_dt;
	}

	if(m_VectorRotation.z >= m_polarDelta)
	{
		m_VectorRotation.z -= 90.0f * m_dt;
	}

	m_position.x += m_magnitude.x * (m_direction.x * m_dt);
	m_position.z += m_magnitude.z * (m_direction.z * m_dt);

	m_position.y = 0;
}

void Enemy::setRange(bool range)
{
	m_mRange = range;
}

void Enemy::combat()
{
	if(m_mRange)
	{
		if(atkChance() == 1)
		{
			dxAmgr.playSound(m_BuffAtk);
			Handler->SendMessage(0.0,-1,0,MSG_ATKCOLLIDE,NULL);
		}
	}

	int HP = getHP();

	if(HP <= 0)
	{
		SetTrigState(4);
	}
}

int Enemy::atkChance()
{
	int atk = 0;

	deltaTime = m_dt;

	accumulator += deltaTime;

	if(accumulator > delayTime)
	{
		atk = 1;

		accumulator = 0;
	}

	return atk;
}

void Enemy::SetTrigState(int trig)
{
	m_trigg = trig;
}

int Enemy::GetTrigState()
{
	return m_trigg;
}

void Enemy::dead()
{
	if(enemydead < 180)
	{
		enemydead += 3.0f;
		m_VectorRotation.x -= enemydead * m_dt;
	}
}

bool Enemy::checkSounds()
{
	if (!m_BuffAwr)
	{
		return false;
	}

	if (!m_BuffAtk)
	{
		return false;
	}

	if (!m_BuffDie)
	{
		return false;
	}

	return true;
}

void Enemy::playAwrSound()
{
	dxAmgr.playSound(m_BuffAwr);
}

void Enemy::playDthSound()
{
	dxAmgr.playSound(m_BuffDie);
}

void Enemy::releaseSounds()
{
	if (m_BuffAwr)
	{
		m_BuffAwr->Release();
		m_BuffAwr = NULL;
	}

	if (m_BuffAtk)
	{
		m_BuffAtk->Release();
		m_BuffAtk = NULL;
	}

	if (m_BuffDie)
	{
		m_BuffDie->Release();
		m_BuffDie = NULL;
	}
}




